#ifndef __TOYM__NET__TIMER_QUEUE_H__
#define __TOYM__NET__TIMER_QUEUE_H__

#include <set>
#include <memory>
#include "Base/Timestamp.h"
#include "net/Callables.h"
#include "net/Channel.h"
#include "Base/noncopyable.h"

namespace toym
{
namespace net
{

class EventLoop;
class Timer;
class TimerId;

class TimerQueue: noncopyable {
public:
    TimerQueue(EventLoop* loop);
    ~TimerQueue();

    TimerId addTimer(const TimerCallback_t& cb, Timestamp when, double interval);
    TimerId addTimer(TimerCallback_t&& cb, Timestamp when, double interval);
    void cancel(const TimerId& timerId);

private:
    void handleTimers();
    void insertTimer(std::shared_ptr<Timer>& timer);
    void updateTimer();
    EventLoop* m_loop;
    std::set<std::pair<Timestamp, std::shared_ptr<Timer>>> m_timers;
    const int m_timerFd;
    Channel m_timerChannel;
    
};


} // namespace net

} // namespace toym


#endif // __TOYM__NET__TIMER_QUEUE_H__